package com.saihai.jiankang.ufTjglXjrw.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.saihai.common.utils.StringUtils;
import com.saihai.jiankang.ufJzg.domain.UfJzg;
import com.saihai.jiankang.ufTjglQxjrw.domain.UfTjglQxjrw;
import com.saihai.jiankang.ufTjglQxjrw.service.IUfTjglQxjrwService;
import com.saihai.jiankang.zzjg.domain.UfZzjg;
import com.saihai.jiankang.zzjg.service.IUfZzjgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.saihai.common.annotation.Log;
import com.saihai.common.core.controller.BaseController;
import com.saihai.common.core.domain.AjaxResult;
import com.saihai.common.enums.BusinessType;
import com.saihai.jiankang.ufTjglXjrw.domain.UfTjglXjrw;
import com.saihai.jiankang.ufTjglXjrw.service.IUfTjglXjrwService;
import com.saihai.common.utils.poi.ExcelUtil;
import com.saihai.common.core.page.TableDataInfo;

/**
 * 体检子任务Controller
 *
 * @author ruoyi
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/jiankang/ufTjglXjrw")
public class UfTjglXjrwController extends BaseController {
    @Autowired
    private IUfTjglXjrwService ufTjglXjrwService;

    @Autowired
    private IUfZzjgService ufZzjgService;
    @Autowired
    private IUfTjglQxjrwService ufTjglQxjrwService;


    /**
     * 查询体检子任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UfTjglXjrw ufTjglXjrw) {

        if (StringUtils.isNotNull(ufTjglXjrw.getCsfw())) {
            UfZzjg ufZzjg = ufZzjgService.selectUfZzjgById(Long.parseLong(ufTjglXjrw.getCsfw()));
            //1市集 2区级
            if (ufZzjg.getLx() < 3) {
                UfZzjg ufZzjg1 = new UfZzjg();
                ufZzjg1.setLx(3L);
                ufZzjg1.setId(Long.parseLong(ufTjglXjrw.getCsfw()));
                List<UfZzjg> list1 = ufZzjgService.selectChildDept(ufZzjg1);
                String ids[] = new String[list1.size()];
                for (int i = 0; i < list1.size(); i++) {
                    ids[i] = String.valueOf(list1.get(i).getId());
                }
                ufTjglXjrw.setCsfws(ids);
                ufTjglXjrw.setCsfw(null);
            }
        }
        if (StringUtils.isNotNull(ufTjglXjrw.getCsfw())) {
            UfZzjg ufZzjg = ufZzjgService.selectUfZzjgById(Long.parseLong(ufTjglXjrw.getCsfw()));
            //1市级 2区级
            if (ufZzjg.getLx() > 3) {
                UfZzjg ufZzjg1 = new UfZzjg();
                ufZzjg1.setLx(3L);
                ufZzjg1.setId(Long.parseLong(ufTjglXjrw.getCsfw()));
                List<UfZzjg> list1 = ufZzjgService.selectParentDept(ufZzjg1);
                ufTjglXjrw.setCsfw(list1.get(0).getId().toString());
            }
        }
        List<UfTjglXjrw> list = null;
        if (StringUtils.isNotNull(ufTjglXjrw.getRoleType()) ) {
            if (ufTjglXjrw.getRoleType().equals("JZG")) {
                list = ufTjglXjrwService.selectZhurw(ufTjglXjrw);
            }else {

                list = ufTjglXjrwService.selectUfTjglXjrwList(ufTjglXjrw);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出体检子任务列表
     */
    @PreAuthorize("@ss.hasPermi('jiankang:ufTjglXjrw:export')")
    @Log(title = "体检子任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfTjglXjrw ufTjglXjrw) {
        List<UfTjglXjrw> list = ufTjglXjrwService.selectUfTjglXjrwList(ufTjglXjrw);
        ExcelUtil<UfTjglXjrw> util = new ExcelUtil<UfTjglXjrw>(UfTjglXjrw.class);
        util.exportExcel(response, list, "体检子任务数据");
    }

    /**
     * 获取体检子任务详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ufTjglXjrwService.selectUfTjglXjrwById(id));
    }

    /**
     * 新增体检子任务
     */
    @PreAuthorize("@ss.hasPermi('jiankang:ufTjglXjrw:add')")
    @Log(title = "体检子任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UfTjglXjrw ufTjglXjrw) {
        return toAjax(ufTjglXjrwService.insertUfTjglXjrw(ufTjglXjrw));
    }

    /**
     * 修改体检子任务
     */
    @PreAuthorize("@ss.hasPermi('jiankang:ufTjglXjrw:edit')")
    @Log(title = "体检子任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UfTjglXjrw ufTjglXjrw) {
        return toAjax(ufTjglXjrwService.updateUfTjglXjrw(ufTjglXjrw));
    }

    /**
     * 删除体检子任务
     */
    @PreAuthorize("@ss.hasPermi('jiankang:ufTjglXjrw:remove')")
    @Log(title = "体检子任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ufTjglXjrwService.deleteUfTjglXjrwByIds(ids));
    }
}
